package com.ttmall.controller;

import com.ttmall.common.pojo.EasyUIDataGridResult;
import com.ttmall.common.pojo.TaotaoResult;
import com.ttmall.service.ItemParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by GalaIO on 2018/1/15.
 */
@Controller
@RequestMapping("/item/param")
public class ItemParamController {

    @Autowired
    private ItemParamService itemParamService;

    @RequestMapping("/list")
    @ResponseBody
    private EasyUIDataGridResult listItemParam(@RequestParam Integer page, @RequestParam Integer rows) {
        return itemParamService.listItemParam(page, rows);
    }

    @RequestMapping("/query/itemcatid/{cid}")
    @ResponseBody
    private TaotaoResult getItemParamByItemCid(@PathVariable Long cid) {
        return itemParamService.getItemParamByItemCid(cid);
    }

    @RequestMapping("/save/{cid}")
    @ResponseBody
    private TaotaoResult insertItemParam(@PathVariable Long cid, @RequestParam String paramData) {
        return itemParamService.insertItemParam(cid, paramData);
    }

    @RequestMapping("/cid/{cid}")
    @ResponseBody
    private TaotaoResult getItemParamByCid(@PathVariable Long cid) {
        return itemParamService.getItemParamByItemCid(cid);
    }

    @RequestMapping("/delete")
    @ResponseBody
    private TaotaoResult deleteItemParam(String ids) {

        if (StringUtils.isNotEmpty(ids)) {
            String[] idSplit = ids.split(",");
            List<Long> idLongs = new ArrayList<>();
            for (String s : idSplit) {
                try {
                    idLongs.add(new Long(s));
                } catch (NumberFormatException ex) {
                    //do nothing
                }
            }
            if (idLongs.size() > 0) {
                return itemParamService.deleteItemParam(idLongs);
            }
        }
        return TaotaoResult.build(400, "error params!");
    }
}
